package com.cxhome.cxinfox.agentProsDiscount;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.cxhome.cxinfox.customer.CustomerService;
import com.cxhome.cxinfox.productvas.ProductService;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.traffic.managent.annotation.ControllerBind;
import com.traffic.managent.base.CxBaseController;
import com.traffic.managent.model.product.Product;
import com.traffic.managent.model.system.Department;
import com.traffic.managent.model.system.UserProsDiscountInfo;
import com.traffic.managent.util.SQLUtils;
import com.traffic.managent.util.StringUtils;
import com.traffic.managent.util.UtilsUtils;

/**
 * 产品折扣管理
 * 
 * @author Guoxing
 *
 */
@ControllerBind(controllerKey = "/system/agentProsDiscount")
public class UserProsDiscountInfoController extends CxBaseController {

	private static final String path = "/pages/system/agentProsDiscount/agentProsDiscount_";

	public void index() {
		list();
	}

	/**
	 * 折扣列表
	 */
	public void list() {
		UserProsDiscountInfo model = getModelByAttr(UserProsDiscountInfo.class);
		// 当前登录用户所属组织机构
		String departmentId = getSessionUser().getStr("departmentId");
		StringBuilder orderSql = new StringBuilder(" order by ");

		// 排序
		String orderBy = getBaseForm().getOrderBy();
		if (StringUtils.isEmpty(orderBy)) {
			orderSql.append(" a.create_time desc");
		} else {
			orderSql.append(orderBy.trim());
		}

		Page<UserProsDiscountInfo> page = UserProsDiscountInfoService.service.paginate(model, getPaginator(),
				departmentId, orderSql.toString());

		setAttr("selectCustomer", CustomerService.service.selectCustomer(model.getStr("customerId")));
		setAttr("page", page);
		setAttr("attr", model);
		render(path + "list.html");
	}
//	public void list() {
//		UserProsDiscountInfo model = getModelByAttr(UserProsDiscountInfo.class);
//		
//		// 当前登录用户所属组织机构
//		String departmentId = getSessionUser().getStr("departmentId");
//		
//		String tempIdsStr = Department.dao.queryChildIds(departmentId);
//		String tempIdsStr1 = tempIdsStr.replaceAll(",", "','");
//		String idsStr = "";
//		if (tempIdsStr1.contains(",")) {
//			idsStr = "('" + tempIdsStr1 + "')";
//		} else {// 组织机构只有一个时查询语句会报错
//			idsStr = "('" + tempIdsStr1 + "','" + tempIdsStr1 + "')";
//		}
//		
//		SQLUtils sql = new SQLUtils(
//				" FROM cx_user_pros_discount_info a, cx_customer_info b, cx_product_vas c , cx_user u  "
//						+ "WHERE a.customerId = b.customerId AND a.del_flag = '0' AND a.proId = c.ids AND c.status = '0'  and u.customerId = b.customerId ");
//		
//		if (model.getAttrValues().length != 0) {
//			sql.whereLike("b.customerId", model.getStr("customerId"));
//			sql.whereLike("c.vasProdName", model.getStr("proId"));
//		}
//		
//		// 排序
//		String orderBy = getBaseForm().getOrderBy();
//		if (StringUtils.isEmpty(orderBy)) {
//			sql.append(" order by  a.create_time desc");
//		} else {
//			sql.append(" order by ").append(orderBy);
//		}
//		
//		Page<UserProsDiscountInfo> page = UserProsDiscountInfo.dao.paginate(getPaginator(),
//				"select DISTINCT a.*,b.customerName, c.vasProdName  ", sql.toString().toString());
//		setAttr("selectCustomer", CustomerService.service.selectCustomer(model.getStr("customerId")));
//		setAttr("page", page);
//		setAttr("attr", model);
//		render(path + "list.html");
//	}

	/**
	 * 跳转新增页面
	 */
	public void add() {
		UserProsDiscountInfo model = getModel(UserProsDiscountInfo.class);
		String productName = getPara("productName");

		// 查询所有平台产品
		List<Product> prodList = ProductService.service.findByParam(productName);

		setAttr("model", model);
		setAttr("page", prodList);
		setAttr("productName", productName);
		setAttr("selectCustomer", CustomerService.service.selectCustomer(""));
		render(path + "add.html");
	}

	public void view() {
		String customerId = getPara();
		String productIds = getPara("productIds");

		List<UserProsDiscountInfo> userProsDiscountInfolist = UserProsDiscountInfoService.service
				.findByParam(customerId, productIds);

		setAttr("userProsDiscountInfolist", userProsDiscountInfolist);
		render(path + "view.html");
	}

	/**
	 * 删除功能:更改当前折扣状态
	 */
	public void delete() {
		UserProsDiscountInfoService.service.delete(getSessionUser().getUserName(), getPara());
		list();
	}

	/**
	 * 跳转编辑页面
	 */
	public void edit() {
		UserProsDiscountInfo model = UserProsDiscountInfo.dao.findById(getPara());
		setAttr("selectCustomer", CustomerService.service.selectCustomer(model.getStr("customerId")));
		setAttr("model", model);
		render(path + "edit.html");
	}
	
	/**
	 * 增加或更新
	 */
	public void save() {
		UserProsDiscountInfo model = getModel(UserProsDiscountInfo.class);
		String ids = getPara();
		String userid = getSessionUser().getUserName();
		String result = UserProsDiscountInfoService.service.saveOrUpdate(model, ids, userid);
		renderMessage(result);
	}

}
